package com.kaiwukj.android.ufamily.mvp.ui.page.home.service;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ServiceResult;

import java.util.List;

public class ServiceItemAdapter extends RecyclerView.Adapter<ServiceItemAdapter.ViewHolder> {

    private int serviceType = 0;
    private Context mContext;
    private List<ServiceResult> mDatas;

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setNewData(List<ServiceResult> data) {
        mDatas = data;
    }

    public ServiceResult getItem(int position) {
        return mDatas.get(position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate((serviceType == 1 || serviceType == 2) ? R.layout.item_service_property : R.layout.item_service, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.tvLabel.setText(mDatas.get(position).getLabel());
        if (serviceType == 1 || serviceType == 2) {
            holder.ivImage.setImageResource(mDatas.get(position).getId());
           /* Glide.with(mContext)
                    .load(mDatas.get(position).getId())
                    .placeholder(R.drawable.picture_image_placeholder)
                    .into(holder.ivImage);*/
        } else {

            if (position > 2) {
                holder.btnExtra.setVisibility(View.VISIBLE);
                holder.tvLabel.setVisibility(View.GONE);
                holder.ivImage.setVisibility(View.GONE);
                holder.btnExtra.setText(mDatas.get(position).getLabel());
                holder.btnExtra.setOnClickListener(v -> {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(null, holder.btnExtra, position);
                    }
                });
            } else {
                Glide.with(mContext)
                        .load(mDatas.get(position).getThumbnail())
                        .placeholder(R.drawable.picture_image_placeholder)
                        .centerCrop()
                        .into(holder.ivImage);
                holder.btnExtra.setVisibility(View.GONE);
                holder.tvLabel.setVisibility(View.VISIBLE);
                holder.ivImage.setVisibility(View.VISIBLE);
            }
        }

        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null)
                onItemClickListener.onItemClick(null, holder.itemView, position);
        });
    }

    public void setServiceType(int type) {
        serviceType = type;
    }

    public int getServiceType() {
        return serviceType;
    }

    @Override
    public int getItemCount() {
        return mDatas == null ? 0 : mDatas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivImage;
        TextView tvLabel;
        Button btnExtra;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivImage = itemView.findViewById(R.id.iv_img);
            tvLabel = itemView.findViewById(R.id.tv_text);
            btnExtra = itemView.findViewById(R.id.tv_text_extra);
        }
    }

}
